<template>
	<view class="content">
		<image class="logo" src="/static/logo.png"></image>
		<view class="text-area">
			<text class="title">{{title}}</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'Hello'
			}
		},
		async onLoad() {
			
			let id,res;
			res = await this.getNav();  //获取分类信息
			console.log(res);
			id = res.data[0].id;
			//根据分类id获取对应分类的新闻列表
			res = await this.getList(id);
			console.log(res);
			id = res.data[2].id;
			//根据新闻的id获取对应的评论
			res = await this.getComment(id);
			console.log(res);
			/*
			//promise链式调用函数
			this.getNav().then(res=>{
				console.log(res);
				let id = res.data[0].id;
				return this.getList(id);
			}).then(res=>{
				console.log(res);
				let id = res.data[2].id;
				return this.getComment(id);
			}).then(res=>{
				console.log(res);
			}).catch(err=>{
				console.log(err);
			});
			*/
			
			
			/*
			//功能封装后嵌套调用
			this.getNav(res=>{
				let id = res.data[0].id;
				this.getList(id,res2=>{
					// console.log(res2);
					let id = res2.data[2].id;
					this.getComment(id,res3=>{
						console.log(res3);
					})
				})
			})
			*/
		},
		methods: {
			
			//封装各请求
			// 1、封装获取分类列表
			getNav(callback){
				return new Promise((resolve,reject)=>{
					uni.request({
						url:'https://ku.qingnian8.com/dataApi/news/navlist.php',
						success: (res) => {
							resolve(res)
						},
						fail: (err) => {
							reject(err)
						}
					})
				})				
			},
			
			//2、封装获取某个分类下的新闻列表
			getList(id){
				return new Promise((resolve,reject)=>{
					uni.request({
						url:'https://ku.qingnian8.com/dataApi/news/newslist.php',
						data:{
							cid:id
						},
						success: (res) => {
							resolve(res);
						},
						fail: (err) => {
							reject(err);
						}
					})
				})
			},
			//3、封装根据某个新闻id，获取该新闻的评论
			getComment(id){
				return new Promise((resolve,reject)=>{
					uni.request({
						url:'https://ku.qingnian8.com/dataApi/news/comment.php',
						data:{
							aid:id
						},
						success: (res) => {
							resolve(res);
						},
						fail: (err) => {
							reject(err)
						}
					})
				})
			}
			
			/*
			getData(){
				//获取分类id
				uni.request({
					url:'https://ku.qingnian8.com/dataApi/news/navlist.php',
					success: (res) => {
						// console.log(res);
						let id = res.data[0].id;	//获取第一个分类id
						uni.request({
							url:'https://ku.qingnian8.com/dataApi/news/newslist.php',
							data:{
								cid:id
							},
							success: (res2) => {
								console.log(res2);
								let id = res2.data[2].id;
								uni.request({
									url:'https://ku.qingnian8.com/dataApi/news/comment.php',
									data:{
										aid:id
									},
									success: (res3) => {
										console.log(res3);
									}
								})
							}
						})
						
					}
				})
			}
			*/
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>
